package com.xm;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @Author：xm
 * @Project：runoob
 * @Date：2024/7/14 18:59
 */

public class LinkedList02 {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("hello");
        linkedList.add("xm");
        linkedList.add("world");
        System.out.println("linkedList = " + linkedList);

        Collections.sort(linkedList);
        System.out.println("linkedList = " + linkedList);

        linkedList.addFirst("aaa");
        System.out.println("linkedList = " + linkedList);

        // 表尾插入元素
        linkedList.add("bbb");
        System.out.println("linkedList = " + linkedList);

        // 移除表头的元素
        linkedList.remove();
        System.out.println("linkedList = " + linkedList);

        System.out.println("linkedList.getFirst() = " + linkedList.getFirst());
        System.out.println("linkedList.getLast() = " + linkedList.getLast());

        for (int size = linkedList.size(), i = 0; i < size; i++) {
            System.out.println("linkedList = " + linkedList.get(i));
        }

        // 删除某一元素
        linkedList.remove("bbb");

        for (String s : linkedList) {
            System.out.println("s = " + s);
        }

        // 返回倒序迭代器
        Iterator<String> iterator = linkedList.descendingIterator();
        while (iterator.hasNext()) {
            System.out.println("iterator.next() = " + iterator.next());
        }
    }
}
